package com.ecarx.camera.utils;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import androidx.annotation.NonNull;

import java.util.ArrayList;

/**
 * RadioButton快速点击切换防呆工具类
 * @author : shuliang.zhang
 * @date : 2023/06/09
 */
public class RadioBtnClickFilterUtils {

    private static final String TAG = "RadioBtnClickFilterUtils";

    /**
     * 默认延迟处理点击切换的毫秒时间
     */
    static final long DEFAULT_DELAY_MS = 500;

    /**
     * 恢复RadioButton可点击状态的消息id
     */
    private static final int MSG_ENABLE_RADIO_BUTTONS = 1;

    /**
     * 所有需要处理延迟点击的RadioButton
     */
    private final ArrayList<RadioButton> mFilterRadioButtons = new ArrayList<>();

    /**
     * 点击延迟处理的毫秒时间
     */
    private final long mClickDelayMs;
    private final Handler mHandler;

    public RadioBtnClickFilterUtils() {
        this(DEFAULT_DELAY_MS);
    }

    public RadioBtnClickFilterUtils(long clickDelayMs) {
        this.mClickDelayMs = clickDelayMs;
        this.mHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                switch (msg.what) {
                    case MSG_ENABLE_RADIO_BUTTONS:
                        LogUtils.d(TAG, "handleMsg MSG_ENABLE_RADIO_BUTTONS, enableAllRadioButtons");
                        enableAllRadioButtons();
                        break;
                    default:
                        break;
                }
            }
        };
    }

    /**
     * 还原所有RadioButton的点击状态
     */
    public void enableAllRadioButtons() {
        LogUtils.d(TAG, "enableAllRadioButtons");
        mHandler.removeMessages(MSG_ENABLE_RADIO_BUTTONS);
        int size = mFilterRadioButtons.size();
        for (int i = 0; i < size; i++) {
            mFilterRadioButtons.get(i).setEnabled(true);
        }
    }

    /**
     * 添加需要处理延迟点击的RadioButton
     * @param radioButton
     * @return
     */
    public RadioBtnClickFilterUtils addRadioButton(RadioButton radioButton) {
        LogUtils.d(TAG, "addRadioButton:" + (radioButton == null ? -1 : radioButton.getId()));
        if (radioButton != null && !mFilterRadioButtons.contains(radioButton)) {
            mFilterRadioButtons.add(radioButton);
            radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        int checkId = radioButton.getId();
                        LogUtils.d(TAG, "onCheckedChanged true, checkId:" + checkId);
                        disableRadioButtonsExcept(checkId);
                        sendDelayEnableMsg();
                    }
                }
            });
        }
        return this;
    }

    /**
     * 禁用除了checkId以外的RadioButton
     * @param checkId
     */
    private void disableRadioButtonsExcept(int checkId) {
        LogUtils.d(TAG, "disableRadioButtonsExcept " + checkId);
        int size = mFilterRadioButtons.size();
        for (int i = 0; i < size; i++) {
            RadioButton radioButton = mFilterRadioButtons.get(i);
            int itemCheckId = radioButton.getId();
            if (checkId != itemCheckId) {
                radioButton.setEnabled(false);
                LogUtils.d(TAG, "disableRadioButton: " + itemCheckId);
            }
        }
    }

    /**
     * 发送延迟恢复RadioButton可点击状态的消息
     */
    private void sendDelayEnableMsg() {
        LogUtils.d(TAG, "sendDelayEnableMsg");
        mHandler.removeMessages(MSG_ENABLE_RADIO_BUTTONS);
        mHandler.sendEmptyMessageDelayed(MSG_ENABLE_RADIO_BUTTONS, mClickDelayMs);
    }

}
